﻿// ***********************************************************************
// Project			: Micua
// Assembly         : Micua.BLL
// Author           : iceStone
// Created          : 2013-11-18 13:12
//
// Last Modified By : iceStone
// Last Modified On : 2013-11-18 13:13
// ***********************************************************************
// <copyright file="BaseService.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Micua.IBLL;
using Micua.IDAL;
using Micua.Model;
using Micua.RepositoryFactory;

namespace Micua.BLL
{
    /// <summary>
    /// 实体服务基类
    /// </summary>
    /// <remarks>
    ///  2013-11-18 20:04 Created By iceStone
    /// </remarks>
    /// <typeparam name="TEntity">实体类型</typeparam>
    public abstract class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, IEntity<TEntity>
    {
        /// <summary>
        /// 实体服务基类构造函数
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        protected BaseService(IBaseRepository<TEntity> repository) { Repository=repository; }
        /// <summary>
        /// 当前操作类
        /// </summary>
        /// <value>The Repository.</value>
        protected IBaseRepository<TEntity> Repository { get; private set; }

        ///// <summary>
        ///// 设置当前操作类
        ///// </summary>
        ///// <remarks>
        /////  2013-11-18 20:04 Created By iceStone
        ///// </remarks>
        //protected abstract void SetCurrentRepository();

        /// <summary>
        /// 向数据库表中插入一个对象记录
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="entity">要插入的实体对象</param>
        /// <returns>插入完成的实体</returns>
        public virtual TEntity Insert(TEntity entity)
        {
            return Repository.Insert(entity);
        }

        /// <summary>
        /// 向数据库中插入一条数据
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="entity">要插入的实体</param>
        /// <param name="commit">是否提交到数据库</param>
        /// <returns>当前实体</returns>
        public virtual TEntity Insert(TEntity entity, bool commit)
        {
            return Repository.Insert(entity, commit);
        }

        /// <summary>
        /// 向数据库中插入多条数据
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="entitys">要插入的实体</param>
        /// <returns>当前实体</returns>
        public virtual IList<TEntity> Insert(params TEntity[] entitys)
        {
            return Repository.Insert(entitys);
        }

        /// <summary>
        /// 向数据库中插入多条数据
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="commit">是否提交到数据库</param>
        /// <param name="entitys">要插入的实体</param>
        /// <returns>当前实体</returns>
        public virtual IList<TEntity> Insert(bool commit, params TEntity[] entitys)
        {
            return Repository.Insert(commit, entitys); 
        }

        /// <summary>
        /// 删除数据库表中的一个实体记录
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="entity">要删除的实体</param>
        /// <returns>执行结果受影响行数</returns>
        public virtual int Delete(TEntity entity)
        {
            return Repository.Delete(entity);
        }

        /// <summary>
        /// 删除数据库中的一条数据
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="entity">要删除的实体</param>
        /// <param name="commit">是否提交到数据库</param>
        /// <returns>执行结果受影响行数</returns>
        public virtual int Delete(TEntity entity, bool commit)
        {
            return Repository.Delete(entity, commit);
        }

        /// <summary>
        /// 批量删除数据库表中对应的数据记录
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="ids">要删除的数据主键ID</param>
        /// <returns>执行结果受影响行数</returns>
        public virtual int Delete(params int[] ids)
        {
            return Repository.Delete(ids);
        }

        /// <summary>
        /// 批量删除数据库中多条数据
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="commit">是否提交到数据库</param>
        /// <param name="ids">要删除的实体Id</param>
        /// <returns>执行结果受影响行数</returns>
        public virtual int Delete(bool commit, params int[] ids)
        {
            return Repository.Delete(commit, ids);
        }

        /// <summary>
        /// 更新数据库表中的一个实体
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="entity">要更新的实体</param>
        /// <returns>执行结果受影响行数</returns>
        public virtual int Update(TEntity entity)
        {
            return Repository.Update(entity);
        }

        /// <summary>
        /// 更新数据库中的一个实体
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="entity">更新的实体</param>
        /// <param name="commit">是否提交到数据库</param>
        /// <returns>执行结果受影响行数</returns>
        public virtual int Update(TEntity entity, bool commit)
        {
            return Repository.Update(entity, commit);
        }

        /// <summary>
        /// 更新数据库中的多个实体
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="entitys">更新的实体</param>
        /// <returns>执行结果受影响行数</returns>
        public virtual int Update(params TEntity[] entitys)
        {
            return Repository.Update(entitys);
        }

        /// <summary>
        /// 更新数据库中的多个实体
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="commit">是否提交到数据库</param>
        /// <param name="entitys">更新的实体</param>
        /// <returns>执行结果受影响行数</returns>
        public virtual int Update(bool commit, params TEntity[] entitys)
        {
            return Repository.Update(commit, entitys);
        }

        /// <summary>
        /// 根据传入委托查询出存在条数
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <returns>存在条数</returns>
        public virtual int QueryCount()
        {
            return Repository.QueryCount();
        }

        /// <summary>
        /// 根据传入委托查询出存在条数
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="predicate">筛选条件表达式</param>
        /// <returns>存在条数</returns>
        public virtual int QueryCount(Expression<Func<TEntity, bool>> predicate)
        {
            return Repository.QueryCount(predicate);
        }

        /// <summary>
        /// 根据传入委托筛选出对应的单个数据实体
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="predicate">筛选条件表达式</param>
        /// <returns>查询到单个数据实体</returns>
        public virtual TEntity QuerySingle(Expression<Func<TEntity, bool>> predicate)
        {
            //Statistics.Instance.QueryCount++;
            return Repository.QuerySingle(predicate);
        }

        /// <summary>
        /// 根据传入委托筛选出对应的数据实体集合
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <returns>查询到数据实体集合</returns>
        public virtual IQueryable<TEntity> Query()
        {
            //Statistics.Instance.QueryCount++;
            return Repository.Query();
        }

        /// <summary>
        /// 根据传入委托筛选出对应的数据实体集合
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="predicate">筛选条件表达式</param>
        /// <returns>查询到数据实体集合</returns>
        public virtual IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> predicate)
        {
            //Statistics.Instance.QueryCount++;
            return Repository.Query(predicate);
        }

        /// <summary>
        /// 根据传入筛选委托和分页条件筛选出对应页面的数据实体集合
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <typeparam name="TField">排序字段类型</typeparam>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">当前页大小</param>
        /// <param name="totalCount">输出查询到的记录数</param>
        /// <param name="predicate">筛选条件委托</param>
        /// <param name="keySelector">排序字段委托</param>
        /// <param name="isDesc">是否降序</param>
        /// <returns>分页查询到数据实体集合</returns>
        public virtual IQueryable<TEntity> QueryPage<TField>(int pageIndex, int pageSize, out int totalCount, Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TField>> keySelector, bool isDesc)
        {
            //Statistics.Instance.QueryCount++;
            return Repository.QueryPage(pageIndex, pageSize, out totalCount, predicate, keySelector, isDesc);
        }
        /// <summary>
        /// 执行一个非查询的T-SQL语句，返回受影响行数
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="sql">T-SQL语句</param>
        /// <param name="parameters">参数列表</param>
        /// <returns>受影响行数</returns>
        public virtual int ExcuteNonQuery(string sql, params object[] parameters)
        {
            //Statistics.Instance.QueryCount++;
            return Repository.ExcuteNonQuery(sql, parameters);
        }
        /// <summary>
        /// 执行一个原始SQL查询，返回泛型类型迭代器
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <param name="sql">T-SQL语句</param>
        /// <param name="parameters">参数列表</param>
        /// <returns>泛型类型迭代器</returns>
        public virtual IEnumerable<TEntity> SqlQuery(string sql, params object[] parameters)
        {
            //Statistics.Instance.QueryCount++;
            return Repository.SqlQuery(sql, parameters);
        }
        /// <summary>
        /// 保存数据库的改变状态
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        /// <returns>受影响行数</returns>
        public int SaveChanges()
        {
            Statistics.Instance.QueryCount++;
            return DbSession.Instance.SaveChanges();
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <remarks>
        ///  2013-11-18 20:04 Created By iceStone
        /// </remarks>
        public void Dispose()
        {
            Repository.Dispose();
        }
    }
}
